package com.yida.gwj.expend.outside.controller;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson2.JSONObject;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.file.FileTypeUtils;
import com.yida.common.core.utils.file.MimeTypeUtils;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.gwj.common.service.IGwjFileService;
import com.yida.system.api.domain.SysFile;

/**
 * @author hmh
 * @date 2023/06/07
 */
@RestController
@RequestMapping("/file")
public class GwjExpendOutsideFileController {

    @Autowired
    private IGwjFileService iGwjFileService;

    @Log(title = "公维金外网文件上传", businessType = BusinessType.UPDATE)
    // @RequiresPermissions("gwj:expend:outside:import")
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return AjaxResult.error("未获取到上传图片信息！请确认后提交！");
        }
        String extension = FileTypeUtils.getExtension(file);
        if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION)) {
            return AjaxResult.error("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
        }
        SysFile sysFile = iGwjFileService.uploadFileDynamicLink(file, null, true);
        return AjaxResult.success("文件上传成功！", sysFile);
    }

    /**
     * 二次上传操作
     * 
     * @param file
     * @param fileName
     * @return
     */
    @Log(title = "公维金外网文件上传", businessType = BusinessType.UPDATE)
    // @RequiresPermissions("gwj:expend:outside:import")
    @PostMapping("/second/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file, String fileName) {
        if (file.isEmpty()) {
            return AjaxResult.error("未获取到上传图片信息！请确认后提交！");
        }
        String extension = FileTypeUtils.getExtension(file);
        if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
            return AjaxResult.error("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
        }
        SysFile sysFile = iGwjFileService.uploadFileDynamicLink(file, fileName, true);
        return AjaxResult.success("文件上传成功！", sysFile);
    }

    @Log(title = "公维金外网文件查看", businessType = BusinessType.UPDATE)
    @GetMapping("/getFileInfo/{fileName}")
    public AjaxResult getFileInfo(@PathVariable String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            return AjaxResult.error("查询文件名不能为空！");
        }
        SysFile sysFile = iGwjFileService.getFileInfo(fileName, true);
        return AjaxResult.success("获取文件成功！", sysFile);
    }

    @Log(title = "公维金外网文件删除", businessType = BusinessType.UPDATE)
    @DeleteMapping
    // public AjaxResult delFile(@PathVariable String fileName) {
    public AjaxResult delFile(@RequestBody JSONObject json) {
        String fileName = json.getString("fileName");
        if (StringUtils.isEmpty(fileName)) {
            return AjaxResult.error("查询文件名不能为空！");
        }
        iGwjFileService.delFile(fileName);
        return AjaxResult.success("文件删除成功！");
    }

}
